
<template>
    <view>
		<!-- #ifdef MP-WEIXIN -->
		<ad :unit-id="setAD.banner" ad-intervals="30" v-if="setAD.banner"></ad>
		<!-- #endif -->
        <view>
            <view class="tab-content">
				<view>
					<view class=" flex flex-direction" style="padding: 10rpx;">
						<button class="cu-btn line-cyan  lg" @click="downloadPhotoAll">保存到手机相册</button>
					</view>
					<view class=" flex flex-direction" style="padding: 10rpx;" >
						<button class="cu-btn line-blue lg" :data-content="videoVo.copywriting" @click="copyContent">复制文案</button>
					</view>
				</view>
				<view class="container">
					<view class="list-box data-v-0034d6f4">
						<view v-for="(item,index) in images" :key="index">
							<image :src="item"
								mode="aspectFill"
								class="image data-v-0034d6f4"
							></image>
							<view class="  flex flex-wrap justify-between align-center bg-white my-btn-view">
								<text class="cu-btn sm" @click="downloadPhoto" :data-url="item">下载</text>
								<text class="cu-btn sm copy" :data-content="item" @click="copyContent">复制链接</text>
							</view>
						</view>
						
					</view>
				</view>
            </view>
        </view>
        
		
    </view>
</template>

<script>


/*
 * Author: 郭少
 * github: https://github.com/guoshao99/wallpaper-applet
 * gitee: https://gitee.com/guoshao99/wallpaper-applet
 * QQ：2549789059
 * 微信：2549789059
 */


const http = require('../../utils/http.js');

const util = require('../../utils/util.js');
	
let setinad = null;

export default {
    components: {
       
    },
    data() {
        return {
			setAd:null,
			videoSrc:'',
			domain:[],
			videoVo:{},
			images:[]
        };
    },
    onLoad: function (options) {
       this.videoSrc = options.videoSrc;
	   
	   this.parseVodei();
	   
	   let that = this;
	   util.getAD(that, function () {
	   	  that.setInterstitialAd(); //加载插屏广告
	   });
    },
   
    onShow() {
       
        var that = this;

    },
    onHide() {
        clearInterval(setinad);
    },
	onShareAppMessage: function() {
		uni.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		});
		return {
			title: '抖音、快手、短视频一键去水印，去水印就是这样简单',
			path: '/pages/index/index',
		};
	},
   
    //转发朋友圈
    onShareTimeline: function () {
        var that = this;
        return {
            title: '抖音、快手、短视频一键去水印，去水印就是这样简单',
            imageUrl: that.shares.share_image
        };
    },
  
    methods: {
        // 获取小程序插屏广告
        setInterstitialAd: function () {
            var that = this;
            if (that.setAD.interstitialid && uni.createInterstitialAd) {
                let interstitialAd = uni.createInterstitialAd({
                    adUnitId: that.setAD.interstitialid
                }); // 监听插屏错误事件
        
                interstitialAd.onError((err) => {
                    console.error(err);
                }); // 显示广告
        
        		  if (interstitialAd) {
        			if (that.setAD.switch_inad == 'yes') {
        			  setinad = setInterval(() => {
        				interstitialAd.show().catch(err => {
        				  console.error(err);
        				});
        			  }, 2000);
        			} else {
        			  setTimeout(() => {
        				interstitialAd.show().catch(err => {
        				  console.error(err);
        				});
        			  }, 6000);
        			}
        		  }
            }
        },
		parseVodei:function(){
			if(this.videoSrc == '' || this.videoSrc == undefined){
				return;
			}
			var that = this;
			
			let data = {
					url : that.videoSrc
				};
			
			uni.showLoading({
				title:'解析中'
			})	
			var params = {
				url: '/video/parse',
				method: 'POST',
				data: data,
				callBack: function (res) {
					
					if(res.code ==  200){
						that.domain = res.data.downDomain;
						that.videoVo = res.data.videoVo;
						that.images = res.data.videoVo.images;
						console.log(that.videoVo.copywriting);
						uni.hideLoading();
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				}
			};
			http.request(params);
		},
		copyContent:function(e){
			
			let content = e.currentTarget.dataset.content;
			uni.setClipboardData({
			    data: content,
			    success: function (res) {
			        uni.showToast({
			            title: '内容复制成功',
			            duration: 2000,
						icon:'none',
			            success(res) {},
			        
			            fail(res) {}
			        });
			    }
			});
		},
		
		saveImage:function(filePath){
			let that = this;
			uni.saveImageToPhotosAlbum({
				filePath: filePath,
				success: function(data) {
					//执行设置缓存方法
					uni.showToast({
						title: '保存成功',
						icon: "success",
						duration: 2000,
						success() {
							
						}
					});
				}
			});
		},
		
		//下载壁纸
		downloadPhoto: function(e) {
			var that = this; 

			var photourl = that.domain[0].value + e.currentTarget.dataset.url;
			
			let fileName = new Date().valueOf();
			
			let  sysPath = wx.env.USER_DATA_PATH + '/' + fileName + '.jpg';
			
			uni.downloadFile({
				url: photourl,
				filePath: sysPath,
				success: function(downSucc) {
					console.log(downSucc);
					let filePath = downSucc.filePath;
					console.log(downSucc);
					uni.showLoading({
						title: '保存中',
						duration: 3000
					});
					uni.saveImageToPhotosAlbum({
						filePath: filePath,
						success: function(succ) {
							console.log(succ);
							//执行设置缓存方法
							uni.showToast({
								title: '保存成功',
								icon: "success",
								duration: 2000,
								success() {
								}
							});
						},
						fail: function(derr) {
							uni.hideLoading();
							if ("saveImageToPhotosAlbum:fail cancel" === derr.errMsg) {
								uni.hideLoading();
								return;
							}
							
							if(derr.errMsg){
								console.log("保存失败，进入授权了");
								uni.openSetting({
									success: function (res) {
										console.log("保存失败，进入授权了-2");
									  uni.authorize({
										scope: 'scope.writePhotosAlbum',
										success(settingdata) {
										  //用户打开了保存图片授权开关
										  that.saveImage(filePath);
										  console.log("进入授权了-3");
										},
										fail(error) {
											console.log(error);
											console.log("拒绝授权");
											  uni.openSetting({
												success: function (data) {
												  //用户打开了保存图片授权开关
												  that.saveImage(filePath);
												},
												fail: function (data) {
												}
											  });
										}
												
									  });
									  },
									  fail: (openErr) => {
									  },
								});
							}
							uni.hideLoading();
						},
						complete: function(dc) {
						}
					});
				},
				fail: function(data) {
					uni.hideLoading();
					uni.showModal({
						title:'提示',
						content:'图片下载失败，请联系平台管理员'
					})
				}
			});
		},
		/**
		 * 批量下载
		 */
		downloadPhotoAll:function(){
			let that = this;
			let images = this.images;
				
			let downCount = 0;
			for (var i = 0; i < images.length; i++) {
				var photourl = that.domain[0].value + images[i];
				
				let fileName = new Date().valueOf();
				
				let  sysPath = wx.env.USER_DATA_PATH + '/' + fileName + '.jpg';
				
				uni.downloadFile({
					url: photourl,
					filePath: sysPath,
					success: function(downSucc) {
						let filePath = downSucc.filePath;
						console.log(downSucc);
						uni.showLoading({
							title: '下载中请稍后',
							duration: 3000
						});
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: function(succ) {
								console.log(succ);
								downCount ++;
								
								if (downCount == images.length) {
									uni.hideLoading();
									uni.showToast({
										title: '保存成功',
										duration: 2000
									});
								}
							},
							fail: function(derr) {
								uni.hideLoading();
								if ("saveImageToPhotosAlbum:fail cancel" === derr.errMsg) {
									uni.hideLoading();
									return;
								}
								
								if(derr.errMsg){
									uni.openSetting({
										success: function (res) {
										  uni.authorize({
											scope: 'scope.writePhotosAlbum',
											success(settingdata) {
											  //用户打开了保存图片授权开关
											  that.saveImage(filePath);
											  console.log("进入授权了-3");
											},
											fail(error) {
												console.log(error);
												console.log("拒绝授权");
												  uni.openSetting({
													success: function (data) {
													  //用户打开了保存图片授权开关
													  that.saveImage(filePath);
													},
													fail: function (data) {
													}
												  });
											}
													
										  });
										  },
										  fail: (openErr) => {
										  },
									});
								}
								uni.hideLoading();
							},
							complete: function(dc) {
							}
						});
					},
					fail: function(data) {
						uni.hideLoading();
					}
				});
				
			}
			
		},
		
		
    }
};
</script>
<style>
@import './images.css';
</style>
